@ECHO off

MODE 80,40

CLS
echo.
echo ----- SMG2 Mod ISO Builder (v0.4)
echo ----- By Humming Owl
echo.

:: ===============================
:: Get to the path the BAT file is
:: ===============================

cd %~dp0

:: ================
:: mods data storage
:: ================

call .\tools\mods_data.bat

:: =============================
:: actions before starting patch
:: =============================

if exist ".\savefiles" (rmdir /s /q ".\savefiles")

if exist ".\temp" (rmdir /s /q ".\temp")

if not exist ".\mods" (mkdir ".\mods")

for /d %%a in (".\mods\*") do rd "%%a" /q /s

if exist ".\*.tmp" (del /S *.tmp >nul)

:: ===================================================
:: Key command to work with variables inside variables
:: ===================================================

SETLOCAL EnableDelayedExpansion

:: =====
:: Tools
:: =====

set WGET=tools\wget.exe
set EXTRACT=tools\7z.exe
set PATCH=tools\patch.exe
set WIT=tools\wit.exe


:: ==========
:: check_file
:: ==========

if not "%~x1" == ".iso" ( 
   if not "%~x1" == ".wbfs" (

      echo.
      echo File dropped IS NOT ISO/WBFS. 
      echo Verify file and try again.
      echo.
      echo Press any key to exit.
      echo.

      pause >nul

      exit
   )
)

%WIT% verify --test --quiet "%~f1"
if %ERRORLEVEL% NEQ 0 (
   echo.
   echo File dropped is ISO/WBFS, but not a valid one.
   echo Verify file and try again.
   echo.
   echo Press any key to exit.
   echo.

   pause >nul

   exit
)

echo.
echo File dropped IS a valid ISO/WBFS.

FOR /F "tokens=*" %%g IN ('%WIT% ID6 "%~f1"') do (set og_gameid=%%g)

if not "%og_gameid%" == "SB4E01" (
   if not "%og_gameid%" == "SB4P01" (
      if not "%og_gameid%" == "SB4J01" (
         if not "%og_gameid%" == "SB4K01" (
            if not "%og_gameid%" == "SB4W01" (
               echo But is not an original SMG2 file.
               echo Verify the file and try again.
               echo.
               echo Press any key to exit.
               echo.

               pause >nul

               exit
            )
         )
      )
   )
)


:: ============
:: region check
:: ============

if "%og_gameid%" == "SB4E01" (set reg=e)
if "%og_gameid%" == "SB4P01" (set reg=p)
if "%og_gameid%" == "SB4J01" (set reg=j)
if "%og_gameid%" == "SB4K01" (set reg=k)
if "%og_gameid%" == "SB4W01" (set reg=w)

:: ============

echo And its an original SMG2 file.
echo Ready to go.
echo.
echo Press any key if you are ready.
echo.

pause

CLS
:: ==================
:: SMG2 Rom Hack List
:: ==================

echo.

echo --- SMG2 Hacks list:
echo.
echo  1. Kaizo Mario Galaxy 2
echo  2. Super Mario Galaxy 2: Cosmic Clones Challenge
echo  3. Super Mario Galaxy 2: The New Green Stars
echo  4. Super Mario Galaxy 2: The Kaizo Green Stars
echo  5. Spinless Mario Galaxy 2 
echo  6. Super Mario Gravity (DEMO)
echo  7. Super Mayro Galaxy
echo  8. Super Mayro Galaxy Twoad
echo  9. Super Mario Galaxy 2.5 (DEMO)
echo 10. Outer Mario Galaxy
echo 11. Super Mario Galaxy Deluxe
echo 12. Super Mario Spectral (SMG2: BlueXD Edition new Demo - E/P/J/K/W)
echo 13. Super Mario Galaxy 2: Daredevil Challenge
echo 14. Neo Mario Galaxy (2020 Build - E/P/J)
echo 15. Super Mario Galaxy 64: Holiday Special (E/P/J)
echo 16. Super Mario Galaxy 2: Fog Edition
echo 17. Super Mario Galaxy 2: The Green Star Festival
echo 18. Super Mario Galaxy 2: Underwater Edition
echo 19. Super Mario Galaxy 2: Master Mode
echo 20. Super Mario Galaxy 2: The Lost Levels (v1.0)
echo 21. Super Mario Starshine (DEMO - E/P/J)
echo 22. Super Mario Galaxy 2: Cosmos Collapse (v1.03R - E/P/J/K/W)
echo 23. Super Mario Galaxy 63 (v1.02 - 30 Star Demo - E/P)
echo 24. Spring Mario Galaxy 2 (v1.0 - E/P/J)
echo 25. Super Mario Galaxy 2: The Spacial Journey (v1.3 - E/P/J)
echo 26. Super Mario Galaxy 2: The Green Star Festival 2 (v1.1 - E/P/J/K/W)
echo 27. Neo Mario Galaxy: The New Green Stars (v1.0 - E/P/J)
echo 28. Super Mario Galaxy 2: Collectors Anxiety (v1.42 - E/P/J - DEMO)
echo 29. Super Mario 3D Galaxy (E/P/J)
echo.

set /P apply_mod=Select Mod number to apply to ISO/WBFS image: 

if %apply_mod%==1 (set mod=KMG2)

if %apply_mod%==2 (set mod=SMG2CCC)

if %apply_mod%==3 (set mod=SMG2TNGS)

if %apply_mod%==4 (set mod=SMG2TKGS)

if %apply_mod%==5 (set mod=SpinMG2)

if %apply_mod%==6 (set mod=SMGRAV)

if %apply_mod%==7 (set mod=SMAYG1)

if %apply_mod%==8 (set mod=SMAYGT)

if %apply_mod%==9 (set mod=SMG25D)

if %apply_mod%==10 (set mod=OUTMG2)

if %apply_mod%==11 (set mod=SMG2DX)

if %apply_mod%==12 (set mod=SMG2BXDE)

if %apply_mod%==13 (set mod=SMG2DC)

if %apply_mod%==14 (set mod=NMG)

if %apply_mod%==15 (set mod=SMG64HS)

if %apply_mod%==16 (set mod=SMG2FE)

if %apply_mod%==17 (set mod=SMG2TGSF)

if %apply_mod%==18 (set mod=SMG2UE)

if %apply_mod%==19 (set mod=SMG2MM)

if %apply_mod%==20 (set mod=SMG2TLL)

if %apply_mod%==21 (set mod=SMSS)

if %apply_mod%==22 (set mod=SMG2CC)

if %apply_mod%==23 (set mod=SMG63)

if %apply_mod%==24 (set mod=SPRINGMG2)

if %apply_mod%==25 (set mod=SMG2TSJ)

if %apply_mod%==26 (set mod=SMG2TGSF2)

if %apply_mod%==27 (set mod=NMGTNGS)

if %apply_mod%==28 (set mod=SMG2CA)

if %apply_mod%==29 (set mod=SMG3D)


::=====================
:: Common build process
::=====================

CLS
echo.
echo ----- !%mod%[0]! Mod ISO Builder
echo ----- !%mod%[1]! !%mod%[2]!
echo.

set /P question1=Download Hack files (approx. size !%mod%[4]!)? (y/n): 
echo.

if %question1%==y GOTO :down_hack

if %question1%==n GOTO :question2

:question2

set /P question2=Already downloaded compressed hack file in the "mods" folder? (y/n): 
echo.

if %question2%==y GOTO :confirm_file

if %question2%==n GOTO :exit_1

:exit_1

echo Download Rom Hack files and try again.
echo Terminating program...
echo Press any key to close program.
echo.

pause >nul

exit

:down_hack

echo Downloading Rom Hack files...
echo.

%WGET% -q --show-progress -t inf "https://archive.org/download/SMG_1-2_Rom_Hacks/!%mod%[3]!" -P mods >nul

echo.

:confirm_file

if exist ".\mods\!%mod%[3]!" GOTO :extract_file

echo Rom Hack file not found.
echo Press any key to close program.
echo.

pause > nul

exit

:extract_file

echo Rom Hack file found.

echo Extracting file contents...

:: =========================================================
:: Win11 issue with double backslash extract path workaround
:: =========================================================

set "extractpath=.\mods\!%mod%[5]!"
set "fixextractpath=%extractpath:\\=\%"

:: =========================================================

%EXTRACT% x ".\mods\!%mod%[3]!" -o"%fixextractpath%" >nul

echo.

:build_iso

echo Building modded disc image...
echo This will take a while.
echo.

echo Extracting image contents...

%WIT% EXTRACT "%~f1" ".\temp" >nul

:: =======================================
:: Redirector to other mods unique options
:: =======================================

if %mod%==SMG2TSJ GOTO :SMG2TSJ_custom

if %mod%==SMG2TGSF2 GOTO :SMG2TGSF2_custom

:: ====================================

echo Copying and replacing mod files...

if exist ".\temp\DATA\" (xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\*" ".\temp\DATA\files\" /E /Y >nul)

if not exist ".\temp\DATA\" (xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\*" ".\temp\files\" /E /Y >nul)

:: ==================================
:: Redirector to mod's unique options
:: ==================================

if %mod%==KMG2 GOTO :KMG2_custom

if %mod%==SMG2TNGS GOTO :SMG2TNGS_custom

if %mod%==SMG2TKGS GOTO :SMG2TKGS_custom

if %mod%==NEOMG2 GOTO :NEOMG2_custom

if %mod%==SMG2BE GOTO :SMG2BE_custom

if %mod%==NMG GOTO :NMG_custom

if %mod%==SMG64HS GOTO :SMG64HS_custom

if %mod%==SMG2BXDE GOTO :SMG2BXDE_custom

if %mod%==SMG2MM GOTO :SMG2MM_custom

if %mod%==SMG2TLL GOTO :SMG2TLL_custom

if %mod%==SMSS GOTO :SMSS_custom

if %mod%==SMG2CC GOTO :SMG2CC_custom

if %mod%==SMG63 GOTO :SMG63_custom

if %mod%==NMGTNGS GOTO :NMGTNGS_custom

if %mod%==SMG2CA GOTO :SMG2CA_custom

if %mod%==SMG3D GOTO :SMG3D_custom

:: ================================== 

:final_steps

echo Assembling game image...

%WIT% COPY ".\temp" ".\!%mod%[0]!.wbfs" >nul

echo Removing temp folders...

rmdir /s /q ".\temp"

rmdir /s /q ".\mods\!%mod%[6]!"

echo Editing game IDs...

%WIT% EDIT --id "!%mod%[8]!" ".\!%mod%[0]!.wbfs" >nul

%WIT% EDIT --tt-id "!%mod%[9]!" ".\!%mod%[0]!.wbfs" >nul

%WIT% EDIT --name "!%mod%[1]!" ".\!%mod%[0]!.wbfs" >nul

if exist ".\!%mod%[0]!.wbfs" GOTO :print-info
if not exist ".\!%mod%[0]!.wbfs" GOTO :print-error

:print-error

echo.
echo Error while modding the image.
echo Please try again.
echo Press any key to continue.
echo.

pause >nul

exit

:print-info

echo.
echo Disc image modded correctly.
echo.
echo Game ID: !%mod%[8]!
echo TMD ID: !%mod%[9]!
echo Game name: !%mod%[1]!
echo.
echo If you want to contribute (New Hack/Improve Tool) 
echo go to my GitHub repository and open an Issue.
echo Have fun^!
echo.
echo Press any key to exit.
echo.

pause >nul

exit


:: ===============================
:: Unique options/commands per mod
:: ===============================

:: ====
:: KMG2
:: ====

:KMG2_custom

echo.

echo Detected Region: %reg%

set /P luigi=Play with Luigi? (y/n):  

echo Applying hex patches... 

if %luigi%==y (set luigipatch=_Luigi.patch)
if %luigi%==n (set luigipatch=.patch)

if %reg%==e (set KMG2[8]=KG2E01)
if %reg%==p (set KMG2[8]=KG2P01)
if %reg%==j (set KMG2[8]=KG2J01)
if %reg%==k (set KMG2[8]=KG2K01)
if %reg%==w (set KMG2[8]=KG2W01)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\%KMG2[8]%%luigipatch%" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\%KMG2[8]%%luigipatch%" >nul)

echo.

GOTO :final_steps

:: ========
:: SMG2TNGS
:: ========

:SMG2TNGS_custom

echo.

echo Detected Region: %reg%

echo.
echo Applying hex patches... 

if %reg%==e (set SMG2TNGS[8]=NGSE01)
if %reg%==p (set SMG2TNGS[8]=NGSP01)
if %reg%==j (set SMG2TNGS[8]=NGSJ01)
if %reg%==k (set SMG2TNGS[8]=NGSK01)
if %reg%==w (set SMG2TNGS[8]=NGSW01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)
if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.
echo This hack works with a 120 star savefile and a txt file.
echo The savefiles need to be applied manually.
echo In the "instructions" folder read the "savefiles.txt" 
echo file to know how to apply the savefiles to the game.
echo.
echo Copying savefiles to "safefiles" folder...
echo.

xcopy ".\mods\SMG2 The New Green Stars\SMG2 The New Green Stars\SaveGame\SB4E01\*" ".\savefiles\" /E /Y >nul

GOTO :final_steps

:: ========
:: SMG2TKGS
:: ========

:SMG2TKGS_custom

echo.
echo This hack works with a 120 stars savefile.
echo The savefiles need to be applied manually.
echo In the "instructions" folder read the "savefiles.txt" 
echo file to know how to apply them.
echo.
echo Copying savefiles to "safefiles" folder...
echo.

xcopy ".\mods\TKGS 1.1.0\SMG2 The Kaizo Green Stars\SaveGame\SB4E01\*" ".\savefiles\" /E /Y >nul

GOTO :final_steps

:: ===
:: NMG
:: ===

:NMG_custom

echo.

echo Detected Region: %reg%

set /P dd=Play in Daredevil mode? (y/n):  

echo Applying hex patches... 

if %dd%==y (set ddpatch=Daredevil.patch)

if %dd%==n (set ddpatch=noDaredevil.patch)

if %reg%==e (set NMG[8]=NMGE01)

if %reg%==p (set NMG[8]=NMGP01)

if %reg%==j (set NMG[8]=NMGJ01)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\%NMG[8]%_%ddpatch%" >nul)

if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\%NMG[8]%_%ddpatch%" >nul)

echo.

GOTO :final_steps

:: =======
:: SMG64HS
:: =======

:SMG64HS_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG64HS[8]=M64E01)

if %reg%==p (set SMG64HS[8]=M64P01)

if %reg%==j (set SMG64HS[8]=M64J01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ========
:: SMG2BXDE
:: ========

:SMG2BXDE_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG2BXDE[8]=BXDE01)

if %reg%==p (set SMG2BXDE[8]=BXDP01)

if %reg%==j (set SMG2BXDE[8]=BXDJ01)

if %reg%==k (set SMG2BXDE[8]=BXDK01)

if %reg%==w (set SMG2BXDE[8]=BXDW01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

:: if exist ".\temp\DATA" (

:: xcopy ".\mods\!%mod%[6]!\Regions\NTSC (USA)\*" ".\temp\DATA\files\" /E /Y >nul

:: xcopy ".\mods\!%mod%[6]!\Regions\PAL (Europe)\*" ".\temp\DATA\files\" /E /Y >nul)

:: if not exist ".\temp\DATA" (

:: xcopy ".\mods\!%mod%[6]!\Regions\NTSC (USA)\*" ".\temp\files\" /E /Y >nul

:: xcopy ".\mods\!%mod%[6]!\Regions\PAL (Europe)\*" ".\temp\files\" /E /Y >nul)

echo.

GOTO :final_steps

:: ======
:: SMG2MM
:: ======

:SMG2MM_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG2MM[8]=SB4MME)

if %reg%==p (set SMG2MM[8]=SB4MMP)

if %reg%==j (set SMG2MM[8]=SB4MMJ)

if %reg%==k (set SMG2MM[8]=SB4MMK)

if %reg%==w (set SMG2MM[8]=SB4MMW)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: =======
:: SMG2TLL
:: =======

:: SMG2TLL_custom (old)

:: echo.

:: echo Detected Region: %reg% 

:: set /P tlldd=Play in Daredevil mode? (y/n): 

:: set /P bluestar=Play with Blue Launch stars? (y/n): 

:: echo Applying hex patches... 

:: if %tlldd%==y (set ddpatch=Daredevil.patch)
:: if %tlldd%==n (set ddpatch=noDaredevil.patch)

:: if %tllreg%==e (set SMG2TLL[8]=TLLE01)
:: if %tllreg%==p (set SMG2TLL[8]=TLLP01)
:: if %tllreg%==j (set SMG2TLL[8]=TLLJ01)

:: if %bluestar%==y (

:: if exist ".\temp\DATA" (

:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\SuperSpinDriver.arc" ".\temp\DATA\files\ObjectData\" /E /Y >nul
:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\SpinDriver.arc" ".\temp\DATA\files\ObjectData\" /E /Y >nul
:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\YellowChip.arc" ".\temp\DATA\files\ObjectData\" /E /Y >nul)

:: if not exist ".\temp\DATA" (

:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\SuperSpinDriver.arc" ".\temp\files\SystemData\" /E /Y >nul
:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\SpinDriver.arc" ".\temp\files\ObjectData\" /E /Y >nul
:: xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SystemData\YellowChip.arc" ".\temp\files\ObjectData\" /E /Y >nul)

:: )

:: if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\%SMG2TLL[8]%_%ddpatch%" >nul)

:: if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\%SMG2TLL[8]%_%ddpatch%" >nul)

:: GOTO :final_steps

:SMG2TLL_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG2TLL[8]=TLLE01)

if %reg%==p (set SMG2TLL[8]=TLLP01)

if %reg%==j (set SMG2TLL[8]=TLLJ01)

if %reg%==k (set SMG2TLL[8]=TLLK01)

if %reg%==w (set SMG2TLL[8]=TLLW01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ====
:: SMSS
:: ====

:SMSS_custom

echo.

echo Detected Region: %reg% 

echo Applying hex patches... 

if %reg%==e (set SMSS[8]=MSSE01)

if %reg%==p (set SMSS[8]=MSSP01)

if %reg%==j (set SMSS[8]=MSSJ01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: ======
:: SMG2CC
:: ======

:SMG2CC_custom

echo.

echo Detected Region: %reg% 

echo Applying hex patches... 

if %reg%==e (set SMG2CC[8]=MCCE01)

if %reg%==p (set SMG2CC[8]=MCCP01)

if %reg%==j (set SMG2CC[8]=MCCJ01)

if %reg%==k (set SMG2CC[8]=MCCK01)

if %reg%==w (set SMG2CC[8]=MCCW01)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: =====
:: SMG63
:: =====

:SMG63_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG63[8]=MG63E1)

if %reg%==p (set SMG63[8]=MG63P1)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps

:: =======
:: SMG2TSJ
:: =======

:SMG2TSJ_custom

echo.
echo 1. Green Plains
echo 2. Pull Path
echo 3. Rocky Gardens
echo 4. Nostalgic Valley
echo.

set /P galaxy=Choose a galaxy (1, 2, 3 or 4): 

echo.
echo Replacing pertinent files... 

if %galaxy%==1 (

if exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\GreenPlains\*" ".\temp\DATA\files\" /E /Y >nul )
if not exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\GreenPlains\*" ".\temp\files\" /E /Y >nul )

)

if %galaxy%==2 (

if exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\PullPath\*" ".\temp\DATA\files\" /E /Y >nul )
if not exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\PullPath\*" ".\temp\files\" /E /Y >nul )

)

if %galaxy%==3 (

if exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RockyGardens\*" ".\temp\DATA\files\" /E /Y >nul )
if not exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RockyGardens\*" ".\temp\files\" /E /Y >nul )

)

if %galaxy%==4 (

if exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\NostalgicValley\*" ".\temp\DATA\files\" /E /Y >nul )
if not exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\NostalgicValley\*" ".\temp\files\" /E /Y >nul )

)

echo.
echo This hack works with a savefile.
echo The savefiles need to be applied manually.
echo In the "instructions" folder read the "savefiles.txt" 
echo file to know how to apply them.
echo.
echo Copying savefiles to "safefiles" folder...
echo.

if %galaxy%==1 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\GreenPlains\SaveGame\*" ".\savefiles\" /E /Y >nul )
if %galaxy%==2 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\PullPath\SaveGame\*" ".\savefiles\" /E /Y >nul )
if %galaxy%==3 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RockyGardens\SaveGame\*" ".\savefiles\" /E /Y >nul )
if %galaxy%==4 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\NostalgicValley\SaveGame\*" ".\savefiles\" /E /Y >nul )

echo.

GOTO :final_steps

:: =========
:: SMG2TGSF2
:: =========

:SMG2TGSF2_custom

echo.
echo 1. Green Star Festival 2
echo 2. Red Star DLC
echo.

set /P dlc=Choose a gamemode (1 or 2): 

echo.
echo Replacing pertinent files... 

if %dlc%==1 GOTO :GSF2gamemode1

if %dlc%==2 GOTO :GSF2gamemode2

:GSF2gamemode1

if exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\tgsf2\*" ".\temp\DATA\files\" /E /Y >nul )
if not exist ".\temp\DATA" ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\tgsf2\*" ".\temp\files\" /E /Y >nul )

echo.

GOTO :GSF2pre_final

:GSF2gamemode2

if exist ".\temp\DATA" (xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RedStarsDLC\*" ".\temp\DATA\files\" /E /Y >nul)
if not exist ".\temp\DATA" (xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RedStarsDLC\*" ".\temp\files\" /E /Y >nul)

echo.

echo Detected Region: %reg%

echo.
echo Applying hex patches... 

if %reg%==e (set SMG2TGSF2[8]=SGF2E1)
if %reg%==p (set SMG2TGSF2[8]=SGF2P1)
if %reg%==j (set SMG2TGSF2[8]=SGF2J1)
if %reg%==k (set SMG2TGSF2[8]=SGF2K1)
if %reg%==w (set SMG2TGSF2[8]=SGF2W1)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)
if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

:GSF2pre_final

echo.
echo This hack works with a savefile.
echo The savefiles need to be applied manually.
echo In the "instructions" folder read the "savefiles.txt" 
echo file to know how to apply them.
echo.
echo Copying savefiles to "safefiles" folder...
echo.

if %dlc%==1 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\tgsf2\SaveGame\*" ".\savefiles\" /E /Y >nul )
if %dlc%==2 ( xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\RedStarsDLC\SaveGame\*" ".\savefiles\" /E /Y >nul )

echo.

GOTO :final_steps

:: =======
:: NMGTNGS
:: =======

:NMGTNGS_custom

echo.

set /P dd=Play in Daredevil mode? (y/n): 

echo Detected Region: %reg%

echo Applying hex patches... 

if %dd%==y (set ddpatch=Daredevil.patch)
if %dd%==n (set ddpatch=noDaredevil.patch)
if %reg%==e (set NMGTNGS[8]=NMGGSE)
if %reg%==p (set NMGTNGS[8]=NMGGSP)
if %reg%==j (set NMGTNGS[8]=NMGGSJ)

if exist ".\temp\DATA" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\%NMGTNGS[8]%_%ddpatch%" >nul)
if not exist ".\temp\DATA" (%PATCH% ".\temp\sys\main.dol" < ".\patches\%NMGTNGS[8]%_%ddpatch%" >nul)

echo.

echo.
echo This hack works with a savefile.
echo The savefiles need to be applied manually.
echo In the "instructions" folder read the "savefiles.txt" 
echo file to know how to apply them.
echo.
echo Copying savefiles to "safefiles" folder...
echo.

xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\SaveGame\*" ".\savefiles\" /E /Y >nul

echo.

GOTO :final_steps


:: ======
:: SMG2CA
:: ======

:SMG2CA_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG2CA[8]=MGCAE1)

if %reg%==p (set SMG2CA[8]=MGCAP1)

if %reg%==j (set SMG2CA[8]=MGCAJ1)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

echo.

GOTO :final_steps


:: =====
:: SMG3D
:: =====

:SMG3D_custom

echo.

echo Detected Region: %reg%

echo Applying hex patches... 

if %reg%==e (set SMG3D[8]=MG3DE1)

if %reg%==p (set SMG3D[8]=MG3DP1)

if %reg%==j (set SMG3D[8]=MG3DJ1)

if exist ".\temp\DATA\" (%PATCH% ".\temp\DATA\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

if not exist ".\temp\DATA\" (%PATCH% ".\temp\sys\main.dol" < ".\patches\!%mod%[8]!.patch" >nul)

:: Replace all language folders with EuEnglish folder contents

xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\EuDutch\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\EuFrench\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\EuGerman\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\EuItalian\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\EuSpanish\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\UsEnglish\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\UsFrench\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\UsSpanish\" /E /Y >nul
xcopy ".\mods\!%mod%[6]!\!%mod%[7]!\LocalizeData\EuEnglish\*" ".\temp\DATA\files\LocalizeData\JpJapanese\" /E /Y >nul

echo.

GOTO :final_steps